package com.css.fxfzypg.modules.rating.library.dao;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.PageUtil;
import com.css.fxfzypg.modules.rating.library.model.dto.KpIndex3DataListDto;
import com.css.fxfzypg.modules.rating.library.model.entity.KpIndex3Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Repository
public class KpIndex3DataRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public void insert(KpIndex3Data kpIndex3Data) {
        String sql = "INSERT INTO kp_index3_data(id, name, remark, is_system, create_time, create_user, update_time, update_user, del_flag, code) values(:id, :name, :remark, :isSystem, :createTime, :createUser, :updateTime, :updateUser, :delFlag, :code)";
        namedParameterJdbcTemplate.update(sql, new BeanPropertySqlParameterSource(kpIndex3Data));
    }

    public void deleteById(String[] ids, String currUid, Date date) {
        String sql = "update kp_index3_data set del_flag = '1', update_time = '" + DateUtil.format(date, "yyyy-MM-dd HH:mm:ss") + "', update_user = '" + currUid + "' where id in ('" + StringUtils.join(ids, "','") + "') ";
        jdbcTemplate.update(sql);
    }

    public void updateById(KpIndex3Data kpIndex3Data) throws Exception {
        if (ObjectUtils.isEmpty(kpIndex3Data.getId())) {
            throw new Exception("id不能为空");
        }
        StringBuilder sql = new StringBuilder("update kp_index3_data set ");
        if (ObjectUtils.isNotEmpty(kpIndex3Data.getName())) {
            sql.append(" name = '").append(kpIndex3Data.getName()).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpIndex3Data.getRemark())) {
            sql.append(" remark = '").append(kpIndex3Data.getRemark()).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpIndex3Data.getUpdateTime())) {
            sql.append(" update_time = '").append(DateUtil.format(kpIndex3Data.getUpdateTime(), "yyyy-MM-dd HH:mm:ss")).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpIndex3Data.getUpdateUser())) {
            sql.append(" update_user = '").append(kpIndex3Data.getUpdateUser()).append("', ");
        }
        sql.deleteCharAt(sql.lastIndexOf(", "));
        sql.append(" where id = '").append(kpIndex3Data.getId()).append("'");
        jdbcTemplate.update(sql.toString());
    }

    public Map<String, Object> list(KpIndex3DataListDto dto) {
        StringBuilder selSql = new StringBuilder("select * from kp_index3_data ");
        StringBuilder countSql = new StringBuilder("select count(*) from kp_index3_data ");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0' ");
        if (ObjectUtils.isNotEmpty(dto.getName())) {
            whereSql.append(" and name like '%").append(dto.getName()).append("%' ");
        }
        selSql.append(whereSql);
        selSql.append(" order by create_time desc ");
        countSql.append(whereSql);
        if (ObjectUtils.isNotEmpty(dto.getPage()) && ObjectUtils.isNotEmpty(dto.getRows())) {
            selSql.append(" limit ").append(Math.max(dto.getRows(), 1)).append(" offset ").append(Math.max(0, ((dto.getPage() - 1) * dto.getRows())));
        }
        List<KpIndex3Data> list = jdbcTemplate.query(selSql.toString(), new BeanPropertyRowMapper(KpIndex3Data.class));
        Integer total = jdbcTemplate.queryForObject(countSql.toString(), Integer.class);
        return new HashMap<String, Object>() {{
            put("list", list);
            put("total", total);
            put("page", dto.getPage());
            put("rows", dto.getRows());
            if (ObjectUtils.isNotEmpty(dto.getPage()) && ObjectUtils.isNotEmpty(dto.getRows())) {
                put("totalPage", PageUtil.totalPage(total, dto.getRows()));
            }
        }};
    }

    public KpIndex3Data getById(String id) {
        try {
            return jdbcTemplate.queryForObject("select * from kp_index3_data where id = '" + id + "'", new BeanPropertyRowMapper<>(KpIndex3Data.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
